package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {


    @Autowired
    RoleMapper mapper;

    /**
     * 添加权限
     *
     * @param id
     * @param permissions
     */
    public void addaddPermission(Integer id, Integer[] permissions) {
        if (permissions != null && permissions.length > 0) {
            for (Integer permission : permissions) {
                mapper.addRoleAndPermission(id, permission);
            }
        }
    }

    /**
     * 分页
     *
     * @param bean
     * @return
     */
    public PageResult findPage(QueryPageBean bean) {
        PageHelper.startPage(bean.getCurrentPage(), bean.getPageSize());
        List<Role> list = mapper.findPage(bean.getQueryString());
        PageInfo pi = new PageInfo(list);
        PageResult result = new PageResult(pi.getTotal(), list);
        return result;
    }

    /**
     * 根据id查询没有的权限
     * @param id
     * @return
     */
    public List<Permission> findById(Integer id) {
        List<Permission> list = mapper.findById(id);
        return list;
    }

}
